package com.example.gmallcoupon.service.impl;

import com.common.to.MemberPrice;
import com.common.to.SkuReductionTo;
import com.example.gmallcoupon.entity.MemberPriceEntity;
import com.example.gmallcoupon.entity.SkuFullReductionEntity;
import com.example.gmallcoupon.entity.SkuLadderEntity;
import com.example.gmallcoupon.service.MemberPriceService;
import com.example.gmallcoupon.service.SkuFullReductionService;
import com.example.gmallcoupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.utils.PageUtils;
import com.common.utils.Query;

import com.example.gmallcoupon.dao.CouponDao;
import com.example.gmallcoupon.entity.CouponEntity;
import com.example.gmallcoupon.service.CouponService;


@Service("couponService")
public class CouponServiceImpl extends ServiceImpl<CouponDao, CouponEntity> implements CouponService {

    @Autowired
    private SkuLadderService skuLadderService;
    @Autowired
    private SkuFullReductionService skuFullReductionService;
    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CouponEntity> page = this.page(
                new Query<CouponEntity>().getPage(params),
                new QueryWrapper<CouponEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReductionInfo(SkuReductionTo skuReductionTo) {
        //满减优惠
        SkuLadderEntity entity = new SkuLadderEntity();
        entity.setSkuId(skuReductionTo.getSkuId());
        entity.setFullCount(skuReductionTo.getFullCount());
        entity.setDiscount(skuReductionTo.getDiscount());
        entity.setAddOther(skuReductionTo.getCountStatus());
        if (skuReductionTo.getFullCount() > 0) {
            skuLadderService.save(entity);
        }

        //满减优惠
        SkuFullReductionEntity fullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionTo, fullReductionEntity);
        if (skuReductionTo.getFullPrice().compareTo(new BigDecimal(0)) == 1) {
            skuFullReductionService.save(fullReductionEntity);
        }


        //会员价格
        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        if (memberPrice != null && memberPrice.size() > 0) {
            List<MemberPriceEntity> collect = memberPrice.stream().map(item -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
                memberPriceEntity.setMemberPrice(item.getPrice());
                memberPriceEntity.setMemberLevelId(item.getId());
                memberPriceEntity.setMemberLevelName(item.getName());
                memberPriceEntity.setAddOther(1);
                return memberPriceEntity;
            }).filter((item) -> {
                //会员价格大于零
                return item.getMemberPrice().compareTo(new BigDecimal(0)) == 1;
            }).collect(Collectors.toList());
            memberPriceService.saveBatch(collect);
        }
    }

}